<?php

class GaAPI_Services_Reports extends GaAPI_Services_Abstract {

    protected $_version = 'v13';
    protected $_serviceName = 'ReportService';

    public function getAll() {
        $result = $this->call('getAllJobs');

        $reportsPage = $this->newPage();
        $reportsPage->setApi($this->getApi());
        $reportsPage->entries = $result['getAllJobsReturn'];

        return $reportsPage;
    }


    public function scheduleJob(GaAPI_Objects_Report_Job $report_job) {
        $valid = $this->validateJob($report_job);
        if (!$valid) {
            return $this->throwError("The report job is not valid.");
        }

        $request_xml  = '<scheduleReportJob>';
        $request_xml .= '<job xmlns:impl="' . $this->getClient()->getNamespace() . '" xsi:type="impl:DefinedReportJob">';
        $request_xml .= $report_job;
        $request_xml .= '</job>';
        $request_xml .= '</scheduleReportJob>';

        $response = $this->call('scheduleReportJob', $request_xml);
        $report_job->id = $response['scheduleReportJobReturn'];
        return $report_job;
    }


    public function validateJob(GaAPI_Objects_Report_Job $report_job) {
        $request_xml  = '<validateReportJob>';
        $request_xml .= '<job xmlns:impl="' . $this->getClient()->getNamespace() . '" xsi:type="impl:DefinedReportJob">';
        $request_xml .= $report_job;
        $request_xml .= '</job>';
        $request_xml .= '</validateReportJob>';

        $response = $this->call('validateReportJob', $request_xml);

        return true;
    }


    public function jobStatus(GaAPI_Objects_Report_Job $report_job) {
        $request_xml  = '<getReportJobStatus>';
        $request_xml .= '<reportJobId>';
        $request_xml .= $report_job->id;
        $request_xml .= '</reportJobId>';
        $request_xml .= '</getReportJobStatus>';

        $response = $this->call('getReportJobStatus', $request_xml);
        return $response['getReportJobStatusReturn'];
    }

    public function jobDownloadURL(GaAPI_Objects_Report_Job $report_job, $gzip = true) {
        $status = $this->jobStatus($report_job);
        if ($status != 'Completed') {
            return $this->throwError("The report job status must be Completed for download URL");
        }

        $fName = ($gzip) ? 'getGzipReportDownloadUrl' : 'getReportDownloadUrl';

        $request_xml  = '<' . $fName . '>';
        $request_xml .= '<reportJobId>';
        $request_xml .= $report_job->id;
        $request_xml .= '</reportJobId>';
        $request_xml .= '</' . $fName . '>';

        $response = $this->call($fName, $request_xml);
        return $response[$fName . 'Return'];
    }

    /**
     *
     * @return GaAPI_Objects_Report_Job
     */
    public function newJob() {
        GaAPI_Autoloader::instance()->load('GaAPI_Objects_Report_Job');
        $report = new GaAPI_Objects_Report_Job();
        return $report;
    }

    /**
     * @return GaAPI_Objects_Report_Page
     */
     public function newPage() {
        GaAPI_Autoloader::instance()->load('GaAPI_Objects_Report_Page');
        $page = new GaAPI_Objects_Report_Page();
        return $page;
    }
}